package com.xls.dormitoryservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xls.base.annotation.Log;
import com.xls.commonutils.Result;
import com.xls.dormitoryservice.entity.Room;
import com.xls.dormitoryservice.service.RoomService;
import com.xls.dormitoryservice.vo.RoomInfoVo;
import com.xls.dormitoryservice.vo.RoomQuery;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xls
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/dormitory/room")
@Api(value = "宿舍管理接口")
@Slf4j
public class RoomController {
    @Autowired
    private RoomService roomService;

    @Log(title = "房间管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('dormitory:room:list')")
    @GetMapping("/getRoom/{id}")
    public Result<Room> getRoom(@PathVariable String id) {
        return Result.success(roomService.getById(id));
    }

    @Log(title = "房间管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('dormitory:room:list','dormitory:roomInfo:list')")
    @PostMapping("/list/{page}/{limit}")
    public Result<Page<Room>> list(@PathVariable Integer page, @PathVariable Integer limit,
                                   @RequestBody RoomQuery roomQuery) {
        return Result.success(roomService.getRoomPage(page, limit, roomQuery));
    }

    @Log(title = "房间管理", businessType = "添加")
    @PreAuthorize("hasAnyAuthority('dormitory:room:save')")
    @PostMapping("/save")
    public Result<Room> save(@RequestBody Room room) {
        return roomService.saveRoom(room) ? Result.success(room) : Result.error();
    }

    @Log(title = "房间管理", businessType = "修改")
    @PreAuthorize("hasAnyAuthority('dormitory:room:update')")
    @PostMapping("/update")
    public Result<Room> update(@RequestBody Room room) {
        return roomService.updateRoom(room) ? Result.success(room) : Result.error();
    }

    @Log(title = "房间管理", businessType = "删除")
    @PreAuthorize("hasAnyAuthority('dormitory:room:remove')")
    @DeleteMapping("/remove/{id}")
    public Result<Room> remove(@PathVariable String id) {
        return roomService.removeById(id) ? Result.success() : Result.error();
    }

    @Log(title = "房间管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('dormitory:roomInfo:list', 'dormitory:allRoomInfo:list')")
    @GetMapping("/getRoomInfoVo/{roomId}")
    public Result<RoomInfoVo> getRoomInfoVo(@PathVariable("roomId") String roomId) {
        return Result.success(roomService.getRoomInfoVo(roomId));
    }

    @Log(title = "房间管理", businessType = "上传图片")
    @PostMapping("/uploadRoomImage")
    public Result<String> uploadRoomImage(@RequestParam("id") String id,
                                          @RequestParam("url") String url) {
        roomService.uploadRoomImage(id, url);
        return Result.success();
    }

    @Log(title = "房间管理", businessType = "删除图片")
    @PostMapping("/removeRoomImage")
    public Result<String> removeRoomImage(@RequestParam("id") String id,
                                          @RequestParam("url") String url) {
        roomService.removeRoomImage(id, url);
        return Result.success();
    }
}

